//
//  LongTagView.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/20.
//

#import "LongTagView.h"

@interface LongTagView()

//最后一个按钮的frame
@property (assign,nonatomic) CGRect frameRect;
@property (nonatomic, strong) NSMutableArray *selectedButArr;
@property (nonatomic, strong) NSMutableArray *selectedButTitleArr;

@end



@implementation LongTagView
#pragma mark - 懒加载 Methods
- (NSMutableArray *)selectedButArr
{
    if (!_selectedButArr) {
        _selectedButArr = [NSMutableArray array];
    }
    return _selectedButArr;
}

- (NSMutableArray *)selectedButTitleArr
{
    if (!_selectedButTitleArr) {
        _selectedButTitleArr = [NSMutableArray array];
    }
    return _selectedButTitleArr;
}


/**
 *  初始化
 *
 *  @param frame    frame
 *  @param tagArray 标签数组
 *
 */
- (instancetype)initWithFrame:(CGRect)frame tagArray:(NSArray*)tagArray{
    
    self = [super initWithFrame:frame];
    if (self) {
        //设置rect的初始值
        self.frameRect = CGRectZero;
        _tagArray = tagArray;
        _maxnumber = tagArray.count;
        self.multiselect = NO;
        [self setUp];
    }
    return self;
}

// 初始化
- (void)setUp{
    
    // 默认颜色
    _textColorNormal = rgba(51, 51, 51, 1);
    _textColorSelected = [UIColor whiteColor];
    _backgroundColorSelected = [UIColor redColor];
    _backgroundColorNormal =  rgba(245, 245, 245, 1);
    _btntag = 0;
    _btnH = 28;
    _textFontNormal = [UIFont systemFontOfSize:14];
    
//    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 120, 30)];
//    self.titleLabel.text = @"afdafsafafad";
//    [self addSubview:self.titleLabel];
//
//    self.deleteBut = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width - 45, 10, 30, 30)];
//    [self.deleteBut setImage:[UIImage imageNamed:@"search_delete"] forState:UIControlStateNormal];
//    [self addSubview:self.deleteBut];
    
    // 创建标签按钮
    [self createTagButton];
}

// 重写set属性
- (void)setTagArray:(NSMutableArray *)tagArray{
    
    _tagArray = tagArray;
    _maxnumber = tagArray.count;
    _btntag = 0;
    // 重新创建标签
    [self.selectedButTitleArr removeAllObjects];
    [self resetTagButton];
}

- (void)setMaxnumber:(NSInteger)maxnumber{
    _maxnumber = maxnumber;
}

- (void)setTextColorNormal:(UIColor *)textColorNormal{
    _textColorNormal = textColorNormal;
    // 重新创建标签
    [self resetTagButton];
}

- (void)setTextColorSelected:(UIColor *)textColorSelected{
    _textColorSelected = textColorSelected;
    // 重新创建标签
    [self resetTagButton];
}

- (void)setTextFontNormal:(UIFont *)textFontNormal{
    _textFontNormal = textFontNormal;
    // 重新创建标签
    [self resetTagButton];
}

- (void)setBtntag:(NSInteger)btntag{
    _btntag = btntag;
    // 重新创建标签
    [self resetTagButton];
}

- (void)setBtnH:(CGFloat)btnH{
    _btnH = btnH;
    // 重新创建标签
    [self resetTagButton];
}


- (void)setBackgroundColorSelected:(UIColor *)backgroundColorSelected{
    
    _backgroundColorSelected = backgroundColorSelected;
    // 重新创建标签
    [self resetTagButton];
}


- (void)setBackgroundColorNormal:(UIColor *)backgroundColorNormal{
    
    _backgroundColorNormal = backgroundColorNormal;
    // 重新创建标签
    [self resetTagButton];
}

#pragma mark - Private

// 重新创建标签
- (void)resetTagButton{
    
    // 移除之前的标签
    for (UIButton* btn  in self.subviews) {
        [btn removeFromSuperview];
    }
    // 重新创建标签
    [self createTagButton];
}

// 创建标签按钮
- (void)createTagButton{
    
    if (!_tagArray.count) {
        return;
    }
    [self.selectedButArr removeAllObjects];
    // 按钮高度
    CGFloat btnH = _btnH;
    if (self.automaticWidth) {
        // 距离左边距
        CGFloat leftX = 5;
        // 距离上边距
        CGFloat topY = 10;
        // 按钮左右间隙
        CGFloat marginX = 5;
        // 按钮上下间隙
        CGFloat marginY = 10;
        // 文字左右间隙
        CGFloat fontMargin = 10;
        
        for (int i = 0; i < _tagArray.count; i++) {
            
            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(marginX + leftX, topY, 100, btnH);
            btn.tag = 100+i;

            
            // 按钮文字
            [btn setTitle:_tagArray[i] forState:UIControlStateNormal];
            
            //------ 默认样式
            [btn setTitleColor:_textColorNormal forState:UIControlStateNormal];
            [btn setTitleColor:_textColorSelected forState:UIControlStateSelected];
            btn.titleLabel.font = _textFontNormal;
            btn.backgroundColor = _backgroundColorNormal;
            // 圆角
            btn.layer.cornerRadius = btn.frame.size.height / 2.f;
            btn.layer.masksToBounds = YES;
            // 边框
            btn.layer.borderColor = _textColorNormal.CGColor;
            btn.layer.borderWidth = 0.5;
            btn.selected = NO;
            if (i==_btntag && !self.multiselect) {
                btn.selected = YES;
                btn.backgroundColor = _backgroundColorSelected;
                btn.layer.borderColor = _textColorSelected.CGColor;
            }
       
        
            
            // 设置按钮的边距、间隙
            [self setTagButtonMargin:btn fontMargin:fontMargin];
            
            // 处理换行
            if (btn.frame.origin.x + btn.frame.size.width + marginX > self.frame.size.width) {
                // 换行
                topY += btnH + marginY;
                
                // 重置
                leftX = 6;
                btn.frame = CGRectMake(marginX + leftX, topY, 100, btnH);
                
                // 设置按钮的边距、间隙
                [self setTagButtonMargin:btn fontMargin:fontMargin];
            }
            
            // 重置高度
            CGRect frame = btn.frame;
            frame.size.height = btnH;
            btn.frame = frame;
            self.frameRect = frame;
            //----- 选中事件
            [btn addTarget:self action:@selector(selectdButton:) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:btn];
            [self.selectedButArr addObject:btn];
            leftX += btn.frame.size.width + marginX;
        }
    }
    else
    {
 
        // 距离左边距
        CGFloat leftX = 5;
        // 距离上边距
        CGFloat topY = 10;
        // 按钮左右间隙
        CGFloat marginX = 5;
        // 按钮上下间隙
        CGFloat marginY = 10;
        // 文字左右间隙
        CGFloat fontMargin = 10;
        
        
        CGFloat btnW = (self.frame.size.width - marginX - (leftX+marginX)*(self.widthNumber-1))/self.widthNumber;
        
        for (int i = 0; i < _tagArray.count; i++) {
            
            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(marginX + leftX, topY, btnW, btnH);
            btn.tag = 100+i;

            
            // 按钮文字
            [btn setTitle:_tagArray[i] forState:UIControlStateNormal];
            
            //------ 默认样式
            [btn setTitleColor:_textColorNormal forState:UIControlStateNormal];
            [btn setTitleColor:_textColorSelected forState:UIControlStateSelected];
            btn.titleLabel.font = _textFontNormal;
            btn.backgroundColor = _backgroundColorNormal;
            // 圆角
            btn.layer.cornerRadius = btn.frame.size.height / 2.f;
            btn.layer.masksToBounds = YES;
            // 边框
            btn.layer.borderColor = _textColorNormal.CGColor;
            btn.layer.borderWidth = 0.5;
            btn.selected = NO;
            if (i==_btntag && !self.multiselect) {
                btn.selected = YES;
                btn.backgroundColor = _backgroundColorSelected;
                btn.layer.borderColor = _textColorSelected.CGColor;
            }
       
            // 处理换行
            if (btn.frame.origin.x + btn.frame.size.width + marginX > self.frame.size.width) {
                // 换行
                topY += btnH + marginY;
                
                // 重置
                leftX = 5;
                btn.frame = CGRectMake(marginX + leftX, topY, btnW, btnH);
            }
            
            // 重置高度
            CGRect frame = btn.frame;
            frame.size.height = btnH;
            btn.frame = frame;
            self.frameRect = frame;
            //----- 选中事件
            [btn addTarget:self action:@selector(selectdButton:) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:btn];
            [self.selectedButArr addObject:btn];
            leftX += btn.frame.size.width + marginX;
        }
    }
    

    
    
    if ([self.Delegate respondsToSelector:@selector(getViewHeight:)])
    {
        [self.Delegate getViewHeight:self.frameRect.origin.y+btnH+10];
    }
}

// 设置按钮的边距、间隙
- (void)setTagButtonMargin:(UIButton*)btn fontMargin:(CGFloat)fontMargin{
    
    // 按钮自适应
    [btn sizeToFit];
    
    // 重新计算按钮文字左右间隙
    CGRect frame = btn.frame;
    frame.size.width += fontMargin*2;
    btn.frame = frame;
}



// 根据颜色生成UIImage
- (UIImage*)imageWithColor:(UIColor*)color{
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 开始画图的上下文
    UIGraphicsBeginImageContext(rect.size);
    
    // 设置背景颜色
    [color set];
    // 设置填充区域
    UIRectFill(CGRectMake(0, 0, rect.size.width, rect.size.height));
    
    // 返回UIImage
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    return image;
    
}

#pragma mark - Event
// 标签按钮点击事件
- (void)selectdButton:(UIButton*)btn{

    if (self.multiselect) {
        if (!btn.selected) {
            if (self.selectedButTitleArr.count==_maxnumber) {
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"最多选择%ld个",_maxnumber]];
                return;
            }
        }
        btn.selected = !btn.selected;
        btn.backgroundColor = btn.selected?_backgroundColorSelected:_backgroundColorNormal;
        btn.layer.borderColor = btn.selected?_textColorSelected.CGColor:_textColorNormal.CGColor;
        [self.selectedButTitleArr removeAllObjects];
        for (UIButton *but in self.selectedButArr) {
            if (but.selected) {
                [self.selectedButTitleArr addObject:but.titleLabel.text];
            }
        }
        NSLog(@"%@",self.selectedButTitleArr);
        if ([self.Delegate respondsToSelector:@selector(DeleteWithTagViewTitleArr:)])
        {
            [self.Delegate DeleteWithTagViewTitleArr:self.selectedButTitleArr];
        }
        
    }
    else{
        _btntag = btn.tag-100;
        // 重新创建标签
        [self resetTagButton];
        if ([self.Delegate respondsToSelector:@selector(DeleteWithTagViewTitle:WhitTag:)])
        {
            [self.Delegate DeleteWithTagViewTitle:btn.titleLabel.text WhitTag:_btntag];
        }
    }


    
}

@end
